var express = require('express');
var router = express.Router();
var ModelMovie = require('../model/Movie');
var ModelMovieType = require('../model/MovieType');
var ModelMovieBasicType = require('../model/MovieBasicType');
var ModelCountry = require('../model/Country');
var bodyParser = require('body-parser');
let md5 = require('js-md5');
// 创建 application/x-www-form-urlencoded 编码解析
// var urlencodedParser = bodyParser.urlencoded({ extended: true })

/* GET users listing. */
// 电影列表
router.post('/get_movie_list', async function(req, res, next) {

	let article_model  = new ModelMovie();
	try{
		let result = await article_model
			.query('movie.id as id,movie.name as name,movie.date as date,country.name as country, movie_type.name as type, movie_basic_type.name as basicType')
			.join({
				type: 'LEFT',
				from: {table_name: 'movie', column: 'type_id'},
				to:   {table_name: 'movie_type', column: 'id'},
			})
			.join({
				type: 'LEFT',
				from: {table_name: 'movie', column: 'basic_type_id'},
				to:   {table_name: 'movie_basic_type', column: 'id'},
			})
			.join({
				type: 'LEFT',
				from: {table_name: 'movie', column: 'country_id'},
				to:   {table_name: 'country', column: 'id'},
			})
			.search();
		res.end(JSON.stringify({
			servertime: new Date().getTime() / 1000,
			ok: true,
			list: result.map(item => ({...item, date: item.date instanceof Date ? item.date.format('yyyy-MM-dd') : null}))
		}));
	}catch(e){
		throw e;
		res.end(JSON.stringify({
			servertime: new Date().getTime() / 1000,
			ok: false
		}));
	}
});

// 获取单个电影
async  function getmovie(req, res, next) {

	let article_model  = new ModelMovie();
	let id = req.body.id || req.query.id;
	try{
		let result = await article_model
			.query('movie.id as id,movie.name as name,movie.date as date,country.name as country, movie_type.name as type, movie_basic_type.name as basicType')
			.join({
				type: 'LEFT',
				from: {table_name: 'movie', column: 'type_id'},
				to:   {table_name: 'movie_type', column: 'id'},
			})
			.join({
				type: 'LEFT',
				from: {table_name: 'movie', column: 'basic_type_id'},
				to:   {table_name: 'movie_basic_type', column: 'id'},
			})
			.join({
				type: 'LEFT',
				from: {table_name: 'movie', column: 'country_id'},
				to:   {table_name: 'country', column: 'id'},
			}).search({
				id,
			});

		res.end(JSON.stringify({
			servertime: new Date().getTime() / 1000,
			ok: true,
			...result[0]
		}));
	}catch(e){
		res.end(JSON.stringify({
			servertime: new Date().getTime() / 1000,
			ok: false
		}));
	}
}
router.all('/get_movie' , getmovie);

// 添加电影
router.post('/add_movie' , async function(req, res, next) {

	let article_model  = new ModelMovie();
	let name = req.body.name || req.query.name || req.fields.name;
	let type_id = req.body.type_id || req.query.type_id || req.fields.type_id;
	let basic_type_id = req.body.basic_type_id || req.query.basic_type_id || req.fields.basic_type_id;
	let country_id = req.body.country_id || req.query.country_id || req.fields.country_id;
	let date = req.body.date || req.query.date || req.fields.date;
	date = new Date((Number(date) || 0) * 1000).format('yyyy-MM-dd');
	let status;
	try{
		let result = await article_model.add({
			name,
			type_id,
			basic_type_id,
			country_id,
			date,
		});
		res.end(JSON.stringify({
			servertime: new Date().getTime() / 1000,
			ok: true
		}));
	}catch(e){
		res.end(JSON.stringify({
			servertime: new Date().getTime() / 1000,
			ok: false
		}));
	}
});

// 编辑电影
router.post('/edit_movie' , async function(req, res, next) {

	let user_model  = new ModelMovie();
	let id = req.body.id;
	let name = req.body.name;
	let type_id = req.body.type_id;
	let basic_type_id = req.body.basic_type_id;
	let country_id = req.body.country_id;
	let date = req.body.date;
	let status;
	try{
		let result = await user_model.update({
			id,
			name,
			type_id,
			basic_type_id,
			country_id,
			date,
		});
		res.end(JSON.stringify({
			servertime: new Date().getTime() / 1000,
			ok: true
		}));
	}catch(e){
		res.end(JSON.stringify({
			servertime: new Date().getTime() / 1000,
			ok: false
		}));
	}
});

// 删除电影
router.post('/delete_movie' , async function(req, res, next) {

	let user_model  = new ModelMovie();
	let id = req.body.id || req.query.id;
	let status;
	try{
		if(!id){

			res.end(JSON.stringify({
				servertime: new Date().getTime() / 1000,
				ok: false,
				error: 'id参数错误，当前为'+id,
			}));
		}
		let result = await user_model.del(id);
		res.end(JSON.stringify({
			servertime: new Date().getTime() / 1000,
			ok: true
		}));
	}catch(e){
		res.end(JSON.stringify({
			servertime: new Date().getTime() / 1000,
			ok: false
		}));
	}
});

// 获取基础类型
router.post('/get_movie_basic_type_list', async function(req, res, next) {

	let basic_type_model  = new ModelMovieBasicType();
	try{
		let result = await basic_type_model.search();
		res.end(JSON.stringify({
			servertime: new Date().getTime() / 1000,
			ok: true,
			list: result
		}));
	}catch(e){
		throw e;
		res.end(JSON.stringify({
			servertime: new Date().getTime() / 1000,
			ok: false
		}));
	}
});

// 获取电影类型
router.post('/get_movie_type_list', async function(req, res, next) {

	let type_model  = new ModelMovieType();
	try{
		let result = await type_model.search();
		res.end(JSON.stringify({
			servertime: new Date().getTime() / 1000,
			ok: true,
			list: result
		}));
	}catch(e){
		throw e;
		res.end(JSON.stringify({
			servertime: new Date().getTime() / 1000,
			ok: false
		}));
	}
});

// 获取国家列表
router.post('/get_country_list', async function(req, res, next) {

	let country_model  = new ModelCountry();
	try{
		let result = await country_model.search();
		res.end(JSON.stringify({
			servertime: new Date().getTime() / 1000,
			ok: true,
			list: result
		}));
	}catch(e){
		throw e;
		res.end(JSON.stringify({
			servertime: new Date().getTime() / 1000,
			ok: false
		}));
	}
});

module.exports = router;
